﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Management;
using System.Threading.Tasks;

namespace P2PTransmit.ViewModal.Service
{
    public class ServiceManager
    {
        //初始化本机资源
        #region Static

        static public IPAddress LocalIPAddress { get; private set; }
        static public string LocalMachineName { get; set; }
        static public string LocalMacAddress { get; private set; }

        static ServiceManager()
        {
            LocalMachineName = Dns.GetHostName();
            var ips = Dns.GetHostAddresses( LocalMachineName );
            LocalIPAddress = ( from e in ips where e.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork select e ).First();
            var Query = new SelectQuery( "select * from CIM_NetworkAdapter" );
            var Searcher = new ManagementObjectSearcher( Query );
            var QueryResult = Searcher.Get();
            foreach ( var i in QueryResult )
            {
                var val = i.GetPropertyValue( "MacAddress" );
                if ( val != null )
                {
                    LocalMacAddress = val.ToString();
                }
            }
        }

        #endregion

        List<object>                                      Services;

        public ServiceManager()
        {
            Services = new List<object>( 8 );
        }

        public void AddService<T>(ProtocolService<T> service, AsyncObservableCollection<T> collection)
        {
            service.CollectionBinding( collection );
            Services.Add( service );
            service.Start();
        }
    }
}
